<?php
/**
 * 《用户反馈》实现类
 *
 * @author 孙晓晔
 * @version $Id: Visit.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Feedback extends InfoUtil {
	public $tbl = Constant::tbl_feedback;

	public $status = array(
		'-' => '请选择',
		'1' => '未答复',
		'2' => '已答复',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'contact' => '联系人',
		'email' => '邮箱',
		'tel' => '电话',

		'1' => '创建时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'emp' => '业务员',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	function tab($controller, $request) {		
		$data = array(		
			'访问日志' => array('cc'=>'log.php?do=LogAccess.find', 'js'=>''),
			'用户反馈' => array('cc'=>'info.php?do=Feedback.find', 'js'=>''),
			'新闻管理' => array('cc'=>'info.php?do=News.find', 'js'=>''),
			'招聘职位' => array('cc'=>'info.php?do=Job.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'info/feedback/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if (strtotime($row['createtime']) < strtotime("-8 hour")) {
			return false;
		}

		return false;
	}

	function _where($request) {
		$ip_s = $request['ip_s'];
		$email_s = $request['email_s'];

		$where = " WHERE 1=1";
		if (!empty($email_s)) {
			$where .= " AND email LIKE '%" . $email_s . "%'";
		}

		if (!empty($ip_s)) {
			$where .= " AND ip = '" . $ip_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/feedback/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/feedback/view');
		return true;
	}
}
?>
